<?php

class Ynadvsearch_AdminSettingsController extends Core_Controller_Action_Admin {
	public function indexAction() {
		$deleteDelRecords = Engine_Api::_()->getApi('settings', 'core')->getSetting('ynadvsearch.deleted', 0);
		if ($deleteDelRecords == 0) {
			$searchModel = new Core_Model_DbTable_Search;
			$results = $searchModel -> fetchAll();
			$deleteMember = "<i>" .Zend_Registry::get('Zend_Translate')->_("Deleted Member"). "</i>";
			$availableTypes = Engine_Api::_()->getItemTypes();
			foreach ($results as $result) {
				if (in_array($result->type, $availableTypes)) {
					$item = $this->view->item($result->type, $result->id);
					if (!$item || $item->getTitle() == $deleteMember) {
						$result->delete();
					}
				}
			}
			Engine_Api::_()->getApi('settings', 'core')->setSetting('ynadvsearch.deleted', 1);
		}
		
		$params = $this -> _getAllParams();
		$this->view->saveSuccess = 0;
		$modulesTable = Engine_Api::_()->getDbtable('modules', 'core');
	    $noneSearchModule = array();
	    $noneSearchModule[] = 'activity';
	    $noneSearchModule[] = 'announcement';
	    $noneSearchModule[] = 'authorization';
	    $noneSearchModule[] = 'core';
	    $noneSearchModule[] = 'fields';
	    $noneSearchModule[] = 'invite';
	    $noneSearchModule[] = 'messages';
	    $noneSearchModule[] = 'network';
	    $noneSearchModule[] = 'payment';
	    $noneSearchModule[] = 'storage';
	    $noneSearchModule[] = 'ynadvsearch';
	    $noneSearchModule[] = 'younet-core';
	    $noneSearchModule[] = 'minify';
	    $noneSearchModule[] = 'advcommentbox';
	    $noneSearchModule[] = 'advmenusystem';
	    $noneSearchModule[] = 'chat';
	    $noneSearchModule[] = 'classified';
	    $noneSearchModule[] = 'contactimporter';
	    $noneSearchModule[] = 'mobi';
	    $noneSearchModule[] = 'ynaffiliate';
	    $noneSearchModule[] = 'webcamavatar';
	    $noneSearchModule[] = 'wall';
	    $noneSearchModule[] = 'profile-completeness';
	    $noneSearchModule[] = 'social-connect';
	    $noneSearchModule[] = 'questionanswer';
	    $noneSearchModule[] = 'blogimporter';
	    $noneSearchModule[] = 'yntheme';
		$searchModulesTable = new Ynadvsearch_Model_DbTable_Modules;
		$select = $modulesTable->select();
	    $select->where('name NOT IN (?)', $noneSearchModule);
	    $results = $modulesTable->fetchAll($select);
	    foreach ($results as $result) {
	    	$searchModulesTable->addModule($result);
	    }
		if(isset($params['submit'])) {
			$mods = $params['moduleynsearch'];
			foreach ($mods as $value => $key) {
				$searchModulesTable->updateEnabledModule($value,$key);
			}
			$this->view->saveSuccess = 1;
		}
		
	    $modules = $searchModulesTable->getModules();
	    $this->view->modules = $modules;
	}

}
